<?php

namespace App\Services\Mission;


use App\Constants\MissionType;
use App\Job\MissionJob;
use App\Model\ChatDepth;
use App\Model\ChatDepthRecord;
use App\Model\Fate;
use App\Model\ReplyRecord;
use App\Model\ReplyStatFateActive;
use App\Model\ReplyStatFatePassive;
use App\Model\ReplyStatNormalActive;
use App\Model\ReplyStatNormalPassive;
use App\Model\ReplyStatTeaseActive;
use App\Model\ReplyStatTeasePassive;
use App\Services\YunXinCloud\WyyClientService;
use App\Utils\LoggerUtil;
use Hyperf\DbConnection\Db;

class MissionQueue
{
    public static function profile($uid, $sex)
    {
        self::enqueue(MissionType::MALE_PROFILE, $uid, $sex);
    }

    public static function headImage($uid, $sex)
    {
        //头像审核通过后才能完成该任务
        //self::enqueue(MissionType::MALE_HEAD_IMAGE, $uid, $sex);
    }

    public static function audio($uid)
    {
        self::enqueue(MissionType::MALE_AUDIO, $uid);
    }

    public static function tease($uid, $sex)
    {
        self::enqueue(MissionType::MALE_TEASE, $uid, $sex);
    }

    public static function call($uid)
    {
        self::enqueue(MissionType::MALE_CALL, $uid);
    }

    public static function reply($uid)
    {
        self::enqueue(MissionType::MALE_REPLY, $uid);
    }

    public static function topic($uid, $sex)
    {
        self::enqueue(MissionType::MALE_TOPIC, $uid, $sex);
    }

    public static function like($uid, $sex)
    {
        self::enqueue(MissionType::MALE_LIKE, $uid, $sex);
    }

    public static function comment($uid, $sex)
    {
        self::enqueue(MissionType::MALE_COMMENT, $uid, $sex);
    }

    public static function uploadVideo($uid, $sex)
    {
        self::enqueue(MissionType::FEMALE_UPLOAD_VIDEO, $uid, $sex);
    }

    public static function uploadAlbum($uid, $sex)
    {
        self::enqueue(MissionType::FEMALE_UPLOAD_ALBUM, $uid, $sex);
    }

    public static function enqueue($type, $uid, $sex = null)
    {
        $data = [
            'type' => $type,
            'uid' => $uid,
            'sex' => $sex,
        ];
        LoggerUtil::write('mission', $data, 'enqueue');
        pushQueueMessage(MissionJob::class, $data);
    }

    public static function fate($send_id, $recv_id)
    {
        // $sql = "SELECT id,reply,msg,ctime FROM tb_fate WHERE send_id=$recv_id AND recv_id=$send_id ORDER BY id DESC LIMIT 1";
        // $rs = $model->findOne($sql);
        $rs = Fate::query()->select('id', 'reply', 'msg', 'ctime')->where('send_id', $recv_id)->where('recv_id', $send_id)->orderBy('id')->first();
        $rs = modelToArray($rs);
        if (!empty($rs)) {
            if ($rs['reply'] == 0) {
                // $model->update($rs['id'], ['reply' => 1], 'tb_fate');
                Fate::query()->where('id', $rs['id'])->update(['reply' => 1]);
                $wyyClient = new WyyClientService();
                $wyyClient->sendMsg($recv_id, $send_id, $rs['msg'], 1, 3, 0, 1, $rs['ctime'] - 2);

            } else if ($rs['reply'] == 1) {
                // $model->update($id, ['reply' => 2], 'tb_fate');
                Fate::query()->where('id', $rs['id'])->update(['reply' => 2]);
            }
        }
    }

    public static function stat($send_id, $recv_id, $chat_type)
    {
        try {
            $time = time();
            $today = strtotime(date('Y-m-d'));
            $user1 = min($send_id, $recv_id);
            $user2 = max($send_id, $recv_id);

            //聊天回复
            $id1 = $id2 = null;

            // $sql = "SELECT id,reply,reply2,reply3,reply4,chat_type FROM tb_reply_record WHERE sender=$send_id AND receiver=$recv_id AND ctime>=$today LIMIT 1";
            // $rs = $model->findOne($sql);
            $rs = ReplyRecord::query()->select('id', 'reply', 'reply2', 'reply3', 'reply4', 'chat_type')
                ->where('sender', $send_id)
                ->where('receiver', $recv_id)
                ->where('ctime', '>=', $today)
                ->first();
            if (empty($rs)) {
                $data = ['sender' => $send_id, 'receiver' => $recv_id, 'chat_type' => $chat_type, 'ctime' => $time];
                // $model->insert($data, 'tb_reply_record');
                ReplyRecord::query()->insert($data);
            } else {
                $rs = modelToArray($rs);
                $id1 = $rs['id'];
                $chat_type1 = $rs['chat_type'];
                if ($rs['reply'] == 1 && $rs['reply2'] == 0) {
                    // $model->update($rs['id'], ['reply2' => 1], 'tb_reply_record');
                    ReplyRecord::query()->where('id', $rs['id'])->update(['reply2' => 1]);
                } else if ($rs['reply3'] == 1 && $rs['reply4'] == 0) {
                    // $model->update($rs['id'], ['reply4' => 1], 'tb_reply_record');
                    ReplyRecord::query()->where('id', $rs['id'])->update(['reply4' => 1]);
                }
            }

            // $sql = "SELECT id,reply,reply2,reply3,reply4,chat_type FROM tb_reply_record WHERE sender=$recv_id AND receiver=$send_id AND ctime>=$today LIMIT 1";
            // $rs = $model->findOne($sql);
            $rs = ReplyRecord::query()->select('id', 'reply', 'reply2', 'reply3', 'reply4', 'chat_type')
                ->where('sender', $recv_id)
                ->where('receiver', $send_id)
                ->where('ctime', '>=', $today)
                ->first();
            if (!empty($rs)) {
                $id2 = $rs['id'];
                $chat_type2 = $rs['chat_type'];
                if ($rs['reply'] == 0) {
                    // $model->update($rs['id'], ['reply' => 1], 'tb_reply_record');
                    ReplyRecord::query()->where('id', $rs['id'])->update(['reply' => 1]);
                } else if ($rs['reply2'] == 1 && $rs['reply3'] == 0) {
                    // $model->update($rs['id'], ['reply3' => 1], 'tb_reply_record');
                    ReplyRecord::query()->where('id', $rs['id'])->update(['reply3' => 1]);
                }
            }

            if (empty($id2)) {
                $type = 'active';
            } else if (empty($id1)) {
                $type = 'passive';
            } else if ($id1 <= $id2) {
                $type = 'active';
            } else {
                $type = 'passive';
            }

            if ($type == 'active') {
                $chat_type = empty($chat_type1) ? $chat_type : $chat_type1;
                // $table = ($chat_type == 3) ? 'fate' : ($chat_type == 5 ? 'tease' : 'normal');
                // $activeTable = "tb_reply_stat_{$table}_active";
                // $passiveTable = "tb_reply_stat_{$table}_passive";
                if ($chat_type == 3) {
                    // fate
                    $activeModel = new ReplyStatFateActive();
                    $passiveModel = new ReplyStatFatePassive();
                } elseif ($chat_type == 5) {
                    // tease
                    $activeModel = new ReplyStatTeaseActive();
                    $passiveModel = new ReplyStatTeasePassive();
                } else {
                    // normal
                    $activeModel = new ReplyStatNormalActive();
                    $passiveModel = new ReplyStatNormalPassive();
                }


                // $sql = "SELECT id,reply,reply2,reply3,reply4 FROM $activeTable WHERE sender=$send_id AND receiver=$recv_id AND ctime>=$today LIMIT 1";
                // $rs = $model->findOne($sql);
                $rs = ReplyRecord::query()->select('id', 'reply', 'reply2', 'reply3', 'reply4')
                    ->where('sender', $send_id)
                    ->where('receiver', $recv_id)
                    ->where('ctime', '>=', $today)
                    ->first();
                if (empty($rs)) {
                    $data = ['sender' => $send_id, 'receiver' => $recv_id, 'ctime' => $time];
                    // $model->insert($data, $activeTable);
                    $activeModel->query()->insert($data);
                } else if ($rs['reply'] == 1 && $rs['reply2'] == 0) {
                    // $model->update($rs['id'], ['reply2' => 1], $activeTable);
                    $activeModel->query()->where('id', $rs['id'])->update(['reply2' => 1]);
                } else if ($rs['reply3'] == 1 && $rs['reply4'] == 0) {
                    // $model->update($rs['id'], ['reply4' => 1], $activeTable);
                    $activeModel->query()->where('id', $rs['id'])->update(['reply4' => 1]);
                }

                // $sql = "SELECT id,reply,reply2,reply3,reply4 FROM $passiveTable WHERE sender=$recv_id AND receiver=$send_id AND ctime>=$today LIMIT 1";
                // $rs = $model->findOne($sql);
                $rs = ReplyRecord::query()->select('id', 'reply', 'reply2', 'reply3', 'reply4')
                    ->where('sender', $recv_id)
                    ->where('receiver', $send_id)
                    ->where('ctime', '>=', $today)
                    ->first();
                if (!empty($rs)) {
                    if ($rs['reply'] == 0) {
                        // $model->update($rs['id'], ['reply' => 1], $passiveTable);
                        $passiveModel->query()->where('id', $rs['id'])->update(['reply' => 1]);
                    } else if ($rs['reply2'] == 1 && $rs['reply3'] == 0) {
                        // $model->update($rs['id'], ['reply3' => 1], $passiveTable);
                        $passiveModel->query()->where('id', $rs['id'])->update(['reply3' => 1]);
                    }
                }
            } else {
                $chat_type = empty($chat_type2) ? $chat_type : $chat_type2;
                // $table = ($chat_type == 3) ? 'fate' : ($chat_type == 5 ? 'tease' : 'normal');
                // $activeTable = "tb_reply_stat_{$table}_active";
                // $passiveTable = "tb_reply_stat_{$table}_passive";
                if ($chat_type == 3) {
                    // fate
                    $activeModel = new ReplyStatFateActive();
                    $passiveModel = new ReplyStatFatePassive();
                } elseif ($chat_type == 5) {
                    // tease
                    $activeModel = new ReplyStatTeaseActive();
                    $passiveModel = new ReplyStatTeasePassive();
                } else {
                    // normal
                    $activeModel = new ReplyStatNormalActive();
                    $passiveModel = new ReplyStatNormalPassive();
                }

                // $sql = "SELECT id,reply,reply2,reply3,reply4 FROM $passiveTable WHERE sender=$send_id AND receiver=$recv_id AND ctime>=$today LIMIT 1";
                // $rs = $model->findOne($sql);
                $rs = ReplyRecord::query()->select('id', 'reply', 'reply2', 'reply3', 'reply4')
                    ->where('sender', $send_id)
                    ->where('receiver', $recv_id)
                    ->where('ctime', '>=', $today)
                    ->first();
                if (empty($rs)) {
                    $data = ['sender' => $send_id, 'receiver' => $recv_id, 'ctime' => $time];
                    // $model->insert($data, $passiveTable);
                    $passiveModel->query()->insert($data);
                } else if ($rs['reply'] == 1 && $rs['reply2'] == 0) {
                    // $model->update($rs['id'], ['reply2' => 1], $passiveTable);
                    $passiveModel->query()->where('id', $rs['id'])->update(['reply2' => 1]);
                } else if ($rs['reply3'] == 1 && $rs['reply4'] == 0) {
                    // $model->update($rs['id'], ['reply4' => 1], $passiveTable);
                    $passiveModel->query()->where('id', $rs['id'])->update(['reply4' => 1]);
                }

                // $sql = "SELECT id,reply,reply2,reply3,reply4 FROM $activeTable WHERE sender=$recv_id AND receiver=$send_id AND ctime>=$today LIMIT 1";
                // $rs = $model->findOne($sql);
                $rs = ReplyRecord::query()->select('id', 'reply', 'reply2', 'reply3', 'reply4')
                    ->where('sender', $recv_id)
                    ->where('receiver', $send_id)
                    ->where('ctime', '>=', $today)
                    ->first();
                if (!empty($rs)) {
                    if ($rs['reply'] == 0) {
                        // $model->update($rs['id'], ['reply' => 1], $activeTable);
                        $activeModel->query()->where('id', $rs['id'])->update(['reply' => 1]);
                    } else if ($rs['reply2'] == 1 && $rs['reply3'] == 0) {
                        // $model->update($rs['id'], ['reply3' => 1], $activeTable);
                        $activeModel->query()->where('id', $rs['id'])->update(['reply3' => 1]);
                    }
                }
            }

            //聊天深度
            // $sql = "SELECT id FROM tb_chat_depth_record WHERE sender=$send_id AND receiver=$recv_id AND ctime>=$today AND depth=0 LIMIT 1";
            // $rs = $model->findOne($sql);
            $rs = ChatDepthRecord::query()->where('sender', $send_id)->where('receiver', $recv_id)->where('ctime', '>=', $today)->where('depth', 0)->first();
            if (!empty($rs)) {
                return;
            }

            // $sql = "SELECT id FROM tb_chat_depth_record WHERE sender=$recv_id AND receiver=$send_id AND ctime>=$today AND depth=0 LIMIT 1";
            // $id = $model->findOneField($sql, 'id');
            $id = ChatDepthRecord::query()
                ->where('depth', 0)
                ->where('sender', $recv_id)
                ->where('receiver', $send_id)
                ->where('ctime', '>=', $today)
                ->value('id');
            if (empty($id)) {
                $data = ['sender' => $send_id, 'receiver' => $recv_id, 'ctime' => $time];
                // $model->insert($data, 'tb_chat_depth_record');
                ChatDepthRecord::query()->insert($data);
            } else {
                // $model->update($id, ['depth' => 1], 'tb_chat_depth_record');
                ChatDepthRecord::query()->where('id', $id)->update(['depth' => 1]);

                // $sql = "SELECT id FROM tb_chat_depth WHERE user1=$user1 AND user2=$user2 LIMIT 1";
                // $id = $model->findOneField($sql, 'id');
                $cid = ChatDepth::query()->where('user1', $user1)->where('user2', $user2)->value('id');
                if (empty($cid)) {
                    $data = ['user1' => $user1, 'user2' => $user2, 'depth' => 1, 'ctime' => $time];
                    // $model->insert($data, 'tb_chat_depth');
                    ChatDepth::query()->insert($data);
                } else {
                    // $sql = "UPDATE tb_chat_depth SET depth=depth+1,ctime=$time WHERE user1=$user1 AND user2=$user2";
                    // $model->execute($sql);
                    ChatDepth::query()->where('id', $cid)->update(['depth' => DB::raw('depth+1'), 'ctime' => $time]);
                }
            }
        } catch (\Throwable $e) {
            LoggerUtil::write('chat_depth', $e->getMessage());
        }
    }
}